AddCSLuaFile("cl_init.lua")
AddCSLuaFile("shared.lua")
include('shared.lua')

function ENT:Initialize()
	self.BaseClass.Initialize(self)

	self.Guns = {}
end

function ENT:Use(ply, caller)
	if (self.LastUsed + 0.5 > CurTime()) then
		self.LastUsed = CurTime()
	else
		if (ply==self.dt.Owner || IsAllied(ply, self.dt.Owner) || !self.Locked) then
			if (activator==self.dt.Owner) then
				self.Locked = true
			end
			self:TransmitGuns(ply)
		else
			Notify(ply,4,3,"This Gun Vault is locked! Use a lock pick to force it open.")
		end
		self.LastUsed = CurTime()
	end
end

function ccWithdrawGun(ply, cmd, args)
	local gunvault = ents.GetByIndex(args[1])
	local gunindex = tonumber(args[2])
	local gunid = tonumber(args[3])
	
	if gunvault then
		if (ply!=self.dt.Owner && !IsAllied(ply, gunvault.dt.Owner) && gunvault.Locked) then return end
		if gunvault:GetPos():Distance(ply:GetPos()) < 110 then
			if gunvault.Guns[gunindex] then
				gunvault:MakeGun(gunid)
				gunvault:WithdrawGun(gunindex)
				gunvault:TransmitGuns(ply)
			end
		else
			Notify(ply, 4, 3, "You are too far away to withdraw guns.")
		end
	end
end
concommand.Add("gunvaultwithdraw", ccWithdrawGun)

function ccGiveGun(ply, cmd, args)
	local gunvault = ents.GetByIndex(args[1])
	local gunindex = tonumber(args[2])
	local gunid = tonumber(args[3])
	
	if gunvault then
		if (ply!=gunvault.dt.Owner || !IsAllied(ply, gunvault.dt.Owner) || gunvault.Locked) then return end
		if gunvault:GetPos():Distance(ply:GetPos()) < 110 then
			if gunvault.Guns[gunindex] then
				local weplist = weapons.GetList()
				if(!IsValid(ply:GetWeapon(weplist[gunid].ClassName))) then
					gunvault:GiveGun(gunid, ply)
					gunvault:WithdrawGun(gunindex)
					gunvault:TransmitGuns(ply)
				else
					Notify(ply, 4, 3, "You already have a " .. weplist[gunid].PrintName .. ".")
				end	
			end
		else
			Notify(ply, 4, 3, "You are too far away to withdraw guns.")
		end
	end
end
concommand.Add("gunvaultgive", ccGiveGun)

// cant use starttouch with guns for some reason.
function ENT:Touch(ent)
	local weplist = weapons.GetList()
	if(table.Count(self.Guns) >= self.MaxGuns) then return end
	if(ent.SpawnTime != nil && ent.SpawnTime > CurTime()) then return end
	if(ent:IsWeapon() && ent.AlreadyIn == nil) then 
		for k, weptable in pairs(weplist) do
			if weptable.ClassName == ent.ClassName then
				self:DepositGun(k)
				ent:Remove()
				ent.AlreadyIn = true
				break
			end
		end
	end
end

function ENT:TransmitGuns(ply)
	umsg.Start("gunvaultmenu", ply)
		umsg.String(glon.encode(self.Guns))
		umsg.Short(self:EntIndex())
	umsg.End()
end

function ENT:DepositGun(gunid)
	table.insert(self.Guns, gunid)
end

function ENT:WithdrawGun(index)
	table.remove(self.Guns, index)
end

function ENT:MakeGun(gunid)
	local weplist = weapons.GetList()
	local gun = ents.Create(weplist[gunid].ClassName)
	gun:SetPos(self:GetPos() + Vector(50, 0, -10))
	gun.SpawnTime = CurTime() + 5
	gun:Spawn()
end

function ENT:GiveGun(gunid, ply)
	local weplist = weapons.GetList()
	ply:Give(weplist[gunid].ClassName)
end

function ENT:SetUnlocked()
	self.Locked = false
end

function ENT:OnRemove()
	for k, id in pairs(self.Guns) do
		self:MakeGun(id)
	end
end